home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part1 / 9712 < prev    next >
Encoding:
Text File  |  1996-08-05  |  2.4 KB  |  63 lines

  1. Path: holly.ACNS.ColoState.EDU!not-for-mail
  2. From: corbyh@holly.ACNS.ColoState.EDU (Corby S. Hudnall)
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: passing arguments to main()
  5. Date: 4 Mar 1996 01:34:57 -0700
  6. Organization: Colorado State University, Fort Collins, CO  80523
  7. Message-ID: <4he9vh$4nu4@holly.ACNS.ColoState.EDU>
  8. References: <4h78vo$nqq@netnews.upenn.edu>
  9. NNTP-Posting-Host: holly.acns.colostate.edu
  10. X-Newsreader: TIN [version 1.2 PL2]
  11.  
  12. Jamie Daniels (daniels@oasis.rad.upenn.edu) wrote:
  13. : Hello,
  14.  
  15. : I have a question about c I am not very good with programming. I have this
  16. : project that I want to take two strings one a name the other being an 80
  17. : character message(sentence). I want it to spit out a simplt print 
  18. statement at
  19. : an Unix prompt. But I don't know how to pass arguments to main(). 
  20. also   I can't : get the output to be entered in at the unix prompt. I 
  21. want the output to be a : command. for a simple example
  22.  
  23. :  printf("ping 165.234.323.222");
  24.  
  25. : will not ping this address it only prints this statement before the prompt
  26.  
  27. You want the "system" command not printf.  Printf only outputs what you 
  28. tell is to.  It doesn't execute anything.  try "man system" or "man 3 
  29. system" so see which header file contains the prototype for system.  I 
  30. think that stdlib.h has it on most systems.  As far as passing in command 
  31. line arguments, use argc and argv.
  32.  
  33. int main(int argc, char* argv[])
  34. {
  35.   for(int i=0;i<argc;i++)
  36.     cout << "Command line arguement" << (i+1) << " is: " << argv[i] << endl;
  37.  
  38.   cout << endl << "Now to ping that address" << endl ;
  39.   system("ping 165.234.323.222"); // Pings the address you specified.  
  40.                                   // This is bad because is won't terminate.
  41.  
  42.   return 0;
  43. }
  44.  
  45. This program prints out all the command line arguements.  The best way to 
  46. learn this is to practice.
  47. argc is an integer value of how many arguements were specified and argv 
  48. is an array of pointers to chars.  Check the online documentation.  This 
  49. is a pretty down and dirty explanation.  You might also look at the 
  50. spawn() and exec() family of functions to execute system commands.
  51.  
  52. // ------------ BEGIN SIGNATURE ---------------
  53. #include <iostream.h>
  54. void main()
  55. {
  56.   cout<<"\aName:\tCorby S. Hudnall\n";
  57.   cout<<"School:\tColorado State University\n";
  58.   cout<<"EMail\tcorbyh@holly.colostate.edu\n";
  59.   cout<<"URL\thttp://holly.colostate.edu/~corbyh/\n";
  60. }
  61. // ------------- END SIGNATURE ----------------
  62.  
  63.